#include "ActionLog.h"
#include "ActionBase.h"
#include "context.h"
#include "util.h"
#include "streamdata.h"
#include "Md5.h"
#include "json.h"
#include "3300_gm.pb.h"


static bool __init__ = ActionLog::Init();

bool ActionLog::Init()
{
    Ctx.msg_router.BindHttpHandle("post", "/log/update_level", ActionLog::HandleUpdateLevel);

    return true;
}

void ActionLog::HandleUpdateLevel(HttpData&& data)
{
    int err = 0;
    std::string body = "";
    do
    {
        Json::Value http_req_json;
        if (!CTools::FromStr(http_req_json, data.body))
        {
            log_error("json parser failed, url:%s, body:%s", data.target.c_str(), data.body.c_str());
            err = 1;
            break;
        }

		std::int32_t sv_type = http_req_json["server_type"].asInt();
		std::int32_t svid =  http_req_json["server_id"].asInt();
		std::int32_t lvl =  http_req_json["level"].asInt();
		
		log_info("sv_type:%d,svid:%d,lvl:%d", sv_type, svid, lvl);
		if (sv_type == 0) 
		{
			body = "server_type not exist";
			break;
		}
		if (lvl == 0)
		{
			body = "level not exist";
			break;
		}

		/*
		gm::LogLevelUpdatePush push;
		push.set_log_lvl(lvl);
		if (svid == 0)
		{
            Ctx.host->MtBroadcastMessageDirect(gm::GM_MSG_ID_LOG_LVL_UPDATE_PUSH, &push, sv_type);
		}
		else
		{
			Ctx.host->SendtoOneSrv(gm::GM_MSG_ID_LOG_LVL_UPDATE_PUSH, &push, sv_type, svid);
		}
		*/
		NETOutputPacket push;
		push.Begin(SET_LOG_LEVEL);
		push.WriteInt(lvl);
		push.End();
		if (svid == 0)
		{
            Ctx.host->MtBroadcastMessageDirect(SET_LOG_LEVEL, std::string(push.packet_buf(),push.packet_size()), sv_type);
		}
		else
		{
			Ctx.host->SendPacket( &push, sv_type, svid);
		}

        body = "ok";
    } while (false);
    ResponseHttp(data.request_id, err, body);
}













